'''
Created on 2013-5-16

@author: zengyu.cw
'''
# -*- coding:utf-8 -*-

import logging
import os

FMT = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(threadName)s - %(message)s")  

def getLogger(loggerName):
    logging.basicConfig(fileMode = "w")
    logger = logging.getLogger(loggerName)
    initFileHandler(logger, "c:\\log", loggerName)
    initStreamHandler(logger)
    return logger

def getFilePath(logRootPath):
    import time
    date = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    os.chdir(logRootPath)
    if not os.path.isdir(date):
        os.mkdir(date)
    return os.path.join(logRootPath, date)

def initFileHandler(logger, logRootPath, loggerName, fmt = FMT, ):
    fh = logging.FileHandler(getFilePath(logRootPath) + os.sep + loggerName + ".log")
    fh.setLevel(logging.ERROR)
    fh.setFormatter(fmt)
    logger.addHandler(fh)

def initStreamHandler(logger, fmt = FMT):
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)
    ch.setFormatter(fmt)
    logger.addHandler(ch)
    

if __name__ == '__main__':
    logger = getLogger("LogFactory")
    logger.warn("abc")
    logger.info("123") 
    print os.sep